<template>
  <div class="login">
    <el-form
      ref="baseForm"
      class="login-form"
      :rules="loginRules"
      :model="loginForm"
    >
      <h3 class="title">LeEn</h3>
      <el-form-item prop="username">
        <el-input type="text" placeholder="账号" v-model="loginForm.username">

        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          placeholder="密码"
          v-model="loginForm.password"
        >

        </el-input>
      </el-form-item>

      <el-form-item prop="cakthacode">
        <el-input
          placeholder="验证码"
          style="width: 63%"
          v-model="loginForm.cakthacode"
        >
        </el-input>
        <div class="login-code">
          <img
            @click="getCodeImg"
            :src="loginForm.codeUrl"
            class="login-code-img"
          />
        </div>
      </el-form-item>

      <el-form-item style="width: 100%">
        <el-button @click="login" @keyup.enter="login" type="primary" :disabled="flag" style="width: 100%">
          <span>{{flag == false?"登录":"登录中...."}}</span>
        </el-button>
      </el-form-item>
    </el-form>

    <div class="el-login-footer">
      <span>LeEn_Learn</span>
    </div>
  </div>
</template>

<script>
import store from '@/store';
export default {
  data() {
    return {
      flag:false,
      loginForm: {
        username: "",
        password: "",
        cakthacode: "",
        codeUrl: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        cakthacode: [
          { required: true, trigger: "blur", message: "请输入验证码" },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.baseForm.validate((valid) => {
        if (!valid) {
          this.$message.warning("你还有信息没填哟");
          return false;
        }
        this.flag = true;
        this.axios({
          method: "post",
          url: "/api/admin/login",
          data: {
            phone: this.loginForm.username,
            password: this.loginForm.password,
            code: this.loginForm.cakthacode,
            phoneCode: this.loginForm.uuid,
          },
        })
          .then((request) => {
            // console.log(request)
            if (request.data.code == 200) {
              //  let token = this.axios.defaults.headers.token=request.data.data.token
               localStorage.setItem('token',request.data.data.token)
              store.commit("settoken", request.data.data.token);
              store.commit("setterUser",request.data.data.user.user)
              // console.log(store.state.user)
              this.$router.replace({ path: "/" });
            }
            if (request.data.code == 400) {
              ElMessage.error(request.data.msg);
              this.getCodeImg();
            }
          })
          .catch((res) => {

            ElMessage({
              showClose: true,
              message: res.response.data.msg,
              type: "error",
            });
          });
      this.flag = false;
      });
    },
    getCodeImg() {
      this.axios({
        method: "get",
        url: "/api/verification",
      }).then((code) => {
        this.loginForm.codeUrl = "data:image/gif;base64," + code.data.img;
        this.loginForm.uuid = code.data.uuid;
      });
    },
  },
  mounted() {
    this.getCodeImg();
  },
};
</script>

<style>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 900px;
  /* background-image: url("/public/logo/logo.png"); */
  background-size: cover;
  background: linear-gradient(
    -67.6938deg,
    rgb(40, 140, 191) 0%,
    rgb(81, 210, 195) 100%
  );
  border-radius: 8.5px;
  overflow: hidden;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background-color: rgba(255, 255, 255, 0.2);
  width: 400px;
  padding: 25px 25px 5px 25px;
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>